#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>

//单进程实例
//int main()
//{
//    printf("before:I am a process, pid:%d, ppid:%d\n",getpid(),getppid());
//   // execl("/usr/bin/ls","ls","-a","-l",NULL);  
//    execlp("ls","ls","-a","-l",NULL);
//    printf("after:I am a process, pid:%d, ppid%d\n",getpid(),getppid());
//    return 0;
//}

//多进程实例
int main()
{
    pid_t id=fork();
    if(id==0){//child
    printf("before:I am a process, pid:%d, ppid:%d\n",getpid(),getppid());
    //sleep(5);
    //execl("/usr/bin/ls","ls","-a","-l",NULL);
    
    //执行自己的程序
    //我们写的程序，无论是什么语言都可以调用，原因：不论什么语言本质都是进程，只要是进程就可以用exec调用
    //execl("./otherExe","otherExe",NULL);
    char *const myargv[]={
        "otherExe",
        "-a",
        "-b",
        "-c",
        NULL
    };
    //execv("./otherExe",myargv);
    char *const myenv[]={
        "MYVLA=1111",
        "MYPATH=/usr/bin/XXX",
        NULL
    };
    execle("./otherExe","otherExe","-a","-w","-v",NULL,myenv);
    //execl("/usr/bin/bash","bash","test.sh",NULL);
    printf("after:I am a process, pid:%d, ppid%d\n",getpid(),getppid());
    exit(0);
    }
    
    //father
    pid_t ret=waitpid(id,NULL,0);
    if(ret>0) printf("wait success, father pid:%d, ret id:%d\n",getpid(),getppid());
    sleep(5);
    return 0;
}
